[BUGF] Fix SequentialWorkflow initialization validation - #1212
Closed
Steve-Dusty wants to merge 3 commits into
Closed
[BUGF] Fix SequentialWorkflow initialization validation#1212Steve-Dusty wants to merge 3 commits into
Steve-Dusty wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SequentialWorkflow was validating agent requirements during init, causing initialization to fail when no agents
were provided. This broke existing tests and prevented valid use cases where users want to instantiate an empty
workflow for later configuration.
Error:
workflow = SequentialWorkflow() # Failed with ValueError
ValueError: Agents list cannot be None or empty
Location: swarms/structs/sequential_workflow.py:104
Root Cause
The reliability_check() method was called in init (line 83), immediately validating that agents exist:
def init(self, ...):
# ... set attributes ...
self.reliability_check() # ← Validation during init
self.flow = self.sequential_flow()
self.agent_rearrange = AgentRearrange(...)
def reliability_check(self):
if self.agents is None or len(self.agents) == 0:
raise ValueError("Agents list cannot be None or empty") # ← Failed here
This violated the principle of separating object construction from usage validation.
Solution
Deferred validation pattern: Allow initialization without agents, validate only when execution methods are called.
Changes Made
Modified init (lines 83-99):
Only validate and initialize if agents are provided
if self.agents is not None and len(self.agents) > 0:
self.reliability_check()
self.flow = self.sequential_flow()
self.agent_rearrange = AgentRearrange(...)
else:
self.flow = ""
self.agent_rearrange = None
Added validation to all execution methods:
def run(self, task: str, ...):
if self.agents is None or len(self.agents) == 0:
raise ValueError("Agents list cannot be None or empty. Add agents before running the workflow.")
Fixed test_sequential_workflow_initialization (lines 7-15):
def test_sequential_workflow_initialization():
workflow = SequentialWorkflow()
assert isinstance(workflow, SequentialWorkflow)
assert workflow.agents is None # ← Was checking workflow.tasks
assert workflow.max_loops == 1
assert workflow.flow == ""
assert workflow.agent_rearrange is None
Updated test_sequential_workflow_error_handling (lines 235-264):
Changed from expecting errors at init to expecting errors at execution:
Initialization is now allowed
workflow_none = SequentialWorkflow(agents=None)
assert workflow_none.agents is None
Error raised when trying to run
with pytest.raises(ValueError, match="Agents list cannot be None or empty"):
workflow_none.run("test task")
Updated parameter table (line 306):
agents| List of agents to execute in sequence | Required |agents| List of agents to execute in sequence | None (optional at init, required before run) |Added new sections:
Updated init documentation:
Testing
All tests pass: ✅ 8 passed, 2 skipped
$ pytest test_sequential_workflow.py -v
test_sequential_workflow_initialization PASSED
test_sequential_workflow_initialization_with_agents PASSED
test_sequential_workflow_multi_agent_execution PASSED
test_sequential_workflow_batched_execution PASSED
test_sequential_workflow_with_multi_agent_collaboration PASSED
test_sequential_workflow_error_handling PASSED
test_sequential_workflow_agent_names_extraction PASSED
test_sequential_workflow_team_awareness PASSED
Test coverage:
Impact
Breaking Changes
None. All existing code continues to work because:
New Behavior
Users can now:
Create empty workflow
workflow = SequentialWorkflow()
Configure later
workflow = SequentialWorkflow(agents=[agent1, agent2])
workflow.run("task")
Attempting to run without agents gives clear error:
ValueError: Agents list cannot be None or empty. Add agents before running the workflow.
Benefits
Files Changed
Checklist
📚 Documentation preview 📚: https://swarms--1212.org.readthedocs.build/en/1212/